package com.ruoyi.web.controller.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.material.entity.request.warehouseAddRequest;
import com.ruoyi.web.controller.material.entity.request.warehousePageRequest;
import com.ruoyi.web.controller.material.entity.ylSupplier;
import com.ruoyi.web.controller.material.entity.ylWarehouse;
import com.ruoyi.web.controller.material.mapper.WarehouseMapper;
import com.ruoyi.web.controller.material.service.WarehouseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 仓库管理Service业务层处理
 *
 * @author jzx
 * @date 2025年10月21日16:01:44
 */
@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;

    /**
     * 列表查询
     *
     * @param warehousePageRequest
     * @return
     */
    @Override
    public List<ylWarehouse> getPage(warehousePageRequest warehousePageRequest) {
        LambdaQueryWrapper<ylWarehouse> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(warehousePageRequest.getWarehouseName())) {
            queryWrapper.like(ylWarehouse::getWarehouseName, warehousePageRequest.getWarehouseName());
        }
        queryWrapper.orderByDesc(ylWarehouse::getCreateTime);
        return warehouseMapper.selectList(queryWrapper);
    }

    /**
     * 删除
     */
    @Override
    public void delete(Long[] id) {
        for (Long dictId : id) {
            warehouseMapper.deleteById(dictId);
        }
    }

    /**
     * 根据id查询
     */
    @Override
    public ylWarehouse selectById(Long id) {
        return warehouseMapper.selectById(id);
    }

    @Override
    public void add(warehouseAddRequest warehouseAddRequest) {
        ylWarehouse ylWarehouse = new ylWarehouse();
        if (warehouseMapper.getWarehouseName(warehouseAddRequest.getWarehouseName(), null)) {
            throw new ServiceException("仓库名称已存在");
        }
        ylWarehouse.setWarehouseName(warehouseAddRequest.getWarehouseName());
        ylWarehouse.setRemark(warehouseAddRequest.getRemark());
        warehouseMapper.insert(ylWarehouse);
    }

    /**
     * 修改仓库
     */
    @Override
    public void update(warehouseAddRequest warehouseUpdateRequest) {
        if (warehouseMapper.getWarehouseName(warehouseUpdateRequest.getWarehouseName(), String.valueOf(warehouseUpdateRequest.getId()))) {
            throw new ServiceException("仓库名称已存在");
        }
        warehouseMapper.update(
                null, // entity 为 null，表示不靠实体更新
                new LambdaUpdateWrapper<ylWarehouse>()
                        .eq(ylWarehouse::getId, warehouseUpdateRequest.getId())
                        .set(ylWarehouse::getWarehouseName, warehouseUpdateRequest.getWarehouseName())
                        .set(ylWarehouse::getRemark, warehouseUpdateRequest.getRemark())
        );
    }
}
